//
//  ShuaWordViewController.m
//  Learn
//
//  Created by apple on 2024/4/18.
//  Copyright © 2024 迟建国. All rights reserved.
//

#import "ShuaWordViewController.h"
#import "ShuaWordItemCell.h"
#import "ShuaWordModel.h"
#import "ShuaWordRequest.h"
#import "MJRefresh.h"
#import "ShuaWordDetailViewController.h"
#import "ShuaWordGenDuViewController.h"

@interface ShuaWordViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/* <#描述#> */
@property (nonatomic, strong) UIView    * leftView;
@property (nonatomic, strong) UILabel    * leftLabel;
@property (nonatomic, strong) UIButton    * bottomBtn;
@property (nonatomic, strong) UIButton    * cancleBtn;

@property (nonatomic, strong) UICollectionView    * myCollectionView;
@property (nonatomic, strong) NSMutableArray    * dataArray;
@property (nonatomic, strong) NSMutableArray    * selectArray;

@property (nonatomic, assign) NSInteger            page;       //
@property (nonatomic, assign) BOOL            isChoose;       //

@end

@implementation ShuaWordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = COLOR_APP_BACKGROUND;
    self.title = @"单词跟读";
    self.page = 1;
    [self initUI];
    [self requestData];
}

- (void)initUI {
    self.cancleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancleBtn.frame = CGRectMake(0, 0, 44, 44);
    [self.cancleBtn setTitle:@"取消" forState:UIControlStateNormal];
    [self.cancleBtn addTarget:self action:@selector(cancleClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.cancleBtn setTitleColor:UIColor.grayColor forState:UIControlStateNormal];
    self.cancleBtn.hidden = YES;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.cancleBtn];
    
    [self.view addSubview:self.leftView];
    [self.view addSubview:self.leftLabel];
    [self.view addSubview:self.bottomBtn];
    [self.view addSubview:self.myCollectionView];
}
- (UIView *)leftView {
    if(!_leftView){
        _leftView = [[UIView alloc]initWithFrame:CGRectMake(10, 14 + NavigationBarHeight, 4, 14 )];
        
        _leftView.backgroundColor = COLOR_TOPIC_SELECT;
    }
    return _leftView;
}
- (UILabel *)leftLabel {
    if(!_leftLabel){
        _leftLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.leftView.right + 5, 0, 200, 22)];
        _leftLabel.centerY = self.leftView.centerY;
//        _leftLabel.text = @"跟读过的单词都记录在这里哦";
        _leftLabel.textColor = COLOR_NOT_SELECT;
        _leftLabel.font = PingFangMediumFont(12);
        _leftLabel.textAlignment = NSTextAlignmentLeft;
        
        NSMutableAttributedString *attributeStr = [[NSMutableAttributedString alloc] initWithString:@"全部 跟读过的单词都记录在这里哦"];
        [attributeStr addAttributes:@{NSForegroundColorAttributeName:COLOR_BLACK,NSFontAttributeName:PingFangMediumFont(16)} range:NSMakeRange(0, 2)];
        _leftLabel.attributedText = attributeStr;
    }
    return _leftLabel;
}
- (UIButton *)bottomBtn {
    if(!_bottomBtn){
        _bottomBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _bottomBtn.frame = CGRectMake(10, SCREEN_H - SafeAreaBottomHeight - 55, SCREEN_W - 20, 45);
        [_bottomBtn setTitle:@"去练习" forState:UIControlStateNormal];
        
        [_bottomBtn setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        _bottomBtn.titleLabel.font  = PingFangMediumFont(18);
        _bottomBtn.az_colors = @[(__bridge id)rgbHex(0x02A1F9, 1).CGColor,(__bridge id)rgbHex(0x29F19C,1).CGColor];
        _bottomBtn.az_startPoint = CGPointMake(0, 0.5);
        _bottomBtn.az_endPoint = CGPointMake(1, 0.5);
        [_bottomBtn js_setRoundedCorners:UIRectCornerAllCorners radii:22.5];
        [_bottomBtn addTarget:self action:@selector(bottomBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _bottomBtn;
}

- (UICollectionView *)myCollectionView {
    if (!_myCollectionView) {
        UICollectionViewFlowLayout *myCollectionLayout = [[UICollectionViewFlowLayout alloc]init];
        myCollectionLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        myCollectionLayout.minimumInteritemSpacing = 10;
        myCollectionLayout.minimumLineSpacing = 10;
        myCollectionLayout.sectionInset = UIEdgeInsetsMake(5, 10, 5, 10);
        myCollectionLayout.itemSize = CGSizeMake((SCREEN_W - 31)/2.0f,50);
        
        _myCollectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 15 + self.leftLabel.bottom, SCREEN_W, self.bottomBtn.top - self.leftLabel.bottom -20) collectionViewLayout:myCollectionLayout];
        _myCollectionView.delegate = self;
        _myCollectionView.dataSource = self;
        
        _myCollectionView.backgroundColor = UIColor.clearColor;
        [_myCollectionView registerClass:ShuaWordItemCell.class forCellWithReuseIdentifier:@"ShuaWordItemCell"];
        
        @weakify(self);
        _myCollectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            @strongify(self);
            self.page = 1;
            [self requestData];
        }];
        _myCollectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            @strongify(self);
            self.page ++;
            [self requestData];
        }];
      
    }
    return _myCollectionView;
}


#pragma mark   lazy
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (NSMutableArray *)selectArray{
    if (!_selectArray) {
        _selectArray = [NSMutableArray array];
    }
    return _selectArray;
}
#pragma mark collectionView delegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ShuaWordItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ShuaWordItemCell" forIndexPath:indexPath];
    cell.isChoose = self.isChoose;
    [cell updateItemCellModel:self.dataArray[indexPath.row]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    ShuaWordModel *model = self.dataArray[indexPath.row];
    if (self.isChoose) {
        ShuaWordItemCell *cell = (ShuaWordItemCell *)[collectionView cellForItemAtIndexPath:indexPath];
        model.isSelect = !model.isSelect;
        if (model.isSelect) {
            [self.selectArray addObject:model];
        }else{
            [self.selectArray removeObject:model];
        }
        [cell updateItemCellModel:model];
    }else{
        ShuaWordDetailViewController *detailVC = [[ShuaWordDetailViewController alloc]init];
        detailVC.wordId = model.ID;
        detailVC.index = indexPath.row;
        detailVC.dataArray = self.dataArray;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}



- (void)bottomBtnClicked:(UIButton *)sender {
    if (!self.isChoose) {
        self.isChoose = YES;
        [self.bottomBtn setTitle:@"开始练习" forState:UIControlStateNormal];
        @weakify(self);
        [self.dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            @strongify(self);
            ShuaWordModel *model = (ShuaWordModel *)obj;
            if (idx < 6) {
                model.isSelect = YES;
                [self.selectArray addObject:model];
            }else{
                *stop = YES;
            }
        }];
        self.cancleBtn.hidden = NO;
        [self.myCollectionView reloadData];
    }else{
     //开始练习
        if (self.selectArray.count > 0) {
            ShuaWordGenDuViewController *genduVC = [[ShuaWordGenDuViewController alloc]init];
            genduVC.dataArray = [self.selectArray mutableCopy];
            [self.navigationController pushViewController:genduVC animated:YES];
            [self resetUI];
        }else{
            [SVProgressHUD showImage:nil status:@"选择联系单词"];
        }
      
    }
}

- (void)cancleClicked:(UIButton *)sender {
    [self resetUI];
}
- (void)resetUI {
    self.cancleBtn.hidden = YES;
    self.isChoose = NO;
    [self.bottomBtn setTitle:@"去练习" forState:UIControlStateNormal];
    [self.selectArray removeAllObjects];
    if (self.dataArray.count > 0) {
        [self.dataArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            ShuaWordModel *model = (ShuaWordModel *)obj;
            model.isSelect = NO;
        }];
    }
    [self.myCollectionView reloadData];
}

- (void)requestData {
    @weakify(self);
    [ShuaWordRequest request_getWordListWithPage:self.page reslut:^(NSMutableArray * _Nonnull array, BOOL isMore, BOOL isRequest) {
        @strongify(self);
        if (isRequest) {
            if (self.page == 1) {
                [self.dataArray removeAllObjects];
            }
            [self.dataArray addObjectsFromArray:array];


            [self.myCollectionView reloadData];
            [self.myCollectionView.mj_header endRefreshing];
            [self.myCollectionView.mj_footer endRefreshing];
            if (!isMore) {
                [self.myCollectionView.mj_footer endRefreshingWithNoMoreData];
            }
        }
    }];
}

@end
